<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<script src="js/mui.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/serverUrl.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
		<link href="css/iconfont.css" rel="stylesheet" />
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">扫一扫</h1>
			<span class="mui-icon mui-icon-spinner-cycle mui-spin mui-pull-right" id="turnTheLight"></span>
		</header>

		<div class="mui-content">
			<div id="bcid">
				<!--盛放扫描控件的div-->
			</div>
		</div>

	</body>
</html>

<script type="text/javascript">
	var height = window.innerHeight + 'px'; //获取页面实际高度   
	var width = window.innerWidth + 'px';
	document.getElementById("bcid").style.height = height;
	document.getElementById("bcid").style.width = width;

	scan = null; //扫描对象
	mui.plusReady(function() { //通过mui初始化扫描 

		mui.init();
		startRecognize();
		
	});

	function startRecognize() { //开启扫描 
		try {
			var filter;
			//自定义的扫描控件样式   
			var styles = {
				frameColor: "#29E52C",
				scanbarColor: "#29E52C",
				background: ""
			}
			//扫描控件构造   
			scan = new plus.barcode.Barcode('bcid', filter, styles);
			scan.onmarked = onmarked;
			scan.onerror = onerror; //扫描错误 
			scan.start();

			//打开关闭闪光灯处理   
			var flag = false;
			document.getElementById("turnTheLight").addEventListener('tap', function() {
				if (flag == false) {
					scan.setFlash(true);
					flag = true;
				} else {
					scan.setFlash(false);
					flag = false;
				}
			});

		} catch (e) {
			mui.toast("出现错误啦:\n" + e);
		}
	};

	function onerror(e) { //错误弹框 
		mui.toast(e);
	};

	function onmarked(type, result) { //这个是扫描二维码的回调函数，type是扫描二维码回调的类型 
		//向服务器请求数据,进行签到
		var tpmc = result.split("?");
		var split = tpmc[1].split("-");
		if (split[0] == "LAB") {
			var returnJson = {};
			returnJson.user = JSON.parse( localStorage.getItem("userInformation") );
			returnJson.id = split[1];
			$.ajax({
				//请求方式
				type: 'POST',
				dataType: 'json',
				url: serverUrl + "sigInLab",
				headers: {
					'Content-Type': 'application/json;charset=utf-8'
				},
				data: JSON.stringify(returnJson),
				//请求成功
				success: function(data) {
					if (data == "success") {
						localStorage.setItem("qiandao","LAB");
						
						//homepage刷新
						var parentPage = plus.webview.getWebviewById('homePage.html');
						mui.fire(parentPage,'refresh1');
						setTimeout(function(){
							mui.fire(parentPage,'refresh2');
							mui.toast("签到成功！");
							mui.back();
						},1000);
					} else{
						mui.toast("当前设备不可用！");
						mui.back();
					}
				},
				//请求失败，包含具体的错误信息
				error: function(e) {
					mui.toast("遇到了未知错误！");
				}
			});
		}else if(split[0] == "PC") {
			var returnJson = {};
			returnJson.user = JSON.parse( localStorage.getItem("userInformation") );
			returnJson.id = split[1];
			$.ajax({
				//请求方式
				type: 'POST',
				dataType: 'json',
				url: serverUrl + "studentSignInComputer",
				headers: {
					'Content-Type': 'application/json;charset=utf-8'
				},
				data: JSON.stringify(returnJson),
				//请求成功
				success: function(data) {
					if (data == "success") {
						localStorage.setItem("qiandao","PC");
						localStorage.setItem("pcID",split[1]);
						
						//homepage刷新
						var parentPage = plus.webview.getWebviewById('homePage.html');
						mui.fire(parentPage,'refresh1');
						setTimeout(function(){
							mui.fire(parentPage,'refresh2');
							mui.toast("签到成功！");
							mui.back();
						},1000);
						
						

					} else{
						mui.toast(data);
						mui.back();
					}
				},
				//请求失败，包含具体的错误信息
				error: function(e) {
					mui.toast("遇到了未知错误！");
				}
			});
		}
	};
</script>
